package com.bjmashibing.system.OSFileIO.io_demo.anotherStream;

import java.io.*;

/**
 * 使用BufferedInputStream包装FileInputStream的好处是：
 * 进行封装可以提高处理效率
 * 可以对字节进行操作，跳过不必要的字节，而不是按照顺序读取和输出
 *
 *
 * 例如abc的数据是：c 官大我 abd   读取出来的是：c å®å¤§æ abd   按照上面的说法可以跳过
 *
 * 但是如何解决乱码呢？
 * 使用BufferedReader再进行一次包装
 *
 */
public class BufferedInputStreamDemo {
    public static void main(String[] args) throws IOException {
        demo1();
//        demo2();
    }

    private static void demo1() throws IOException {
        File file = new File("src/Java_IO/io_demo/Resources/abc.txt");
        FileInputStream inputStream = new FileInputStream(file);
        BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
        int length=0;
        while ((length=bufferedInputStream.read())!=-1){
//            bufferedInputStream.skip(10);
            System.out.print((char) length);
        }
        bufferedInputStream.close();
        inputStream.close();

    }


    private static void demo2() throws IOException {
        File file = new File("src/Java_IO/io_demo/Resources/abc.txt");
        FileInputStream inputStream = new FileInputStream(file);
        BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
        BufferedReader reader=new BufferedReader(new InputStreamReader(bufferedInputStream));
        int length=0;
        while ((length=reader.read())!=-1){
            System.out.print((char) length);
        }
        reader.close();
        bufferedInputStream.close();
        inputStream.close();

    }

}
